package top.lilele.adminSystem.web;

import cn.hutool.core.lang.tree.Tree;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lilele.adminSystem.dto.AddUserDepartmentDto;
import top.lilele.adminSystem.dto.DeptUserListDto;
import top.lilele.adminSystem.dto.GetUserByDeptIdDto;
import top.lilele.adminSystem.dto.IdParams;
import top.lilele.adminSystem.entity.AdminDepartmentEntity;
import top.lilele.adminSystem.entity.AdminUserDepartmentEntity;
import top.lilele.adminSystem.exception.business.DepartmentHasChildrenException;
import top.lilele.adminSystem.service.AdminDepartmentService;
import top.lilele.adminSystem.utils.BaseResponse;
import top.lilele.adminSystem.utils.ResponseCodeEnum;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lilele
 */
@RestController
@RequestMapping(value = "department")
public class AdminDepartmentController {
    @Resource
    AdminDepartmentService departmentService;

    @PostMapping(value = "add")
    public BaseResponse add(@RequestBody @Validated AdminDepartmentEntity adminDepartmentEntity) {
        String id = departmentService.add(adminDepartmentEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, id);
    }

    @PostMapping(value = "selectAllDepartment")
    public BaseResponse selectAllDepartment() {
        List<Tree<String>> trees = departmentService.selectAllDepartment();
        return BaseResponse.info(ResponseCodeEnum.Success, trees);
    }

    @PostMapping(value = "selectUserListByDeptId")
    public BaseResponse selectUserListByDeptId(@RequestBody @Validated GetUserByDeptIdDto getUserByDeptIdDto) {
        List<DeptUserListDto> list = departmentService.selectUserListByDeptId(getUserByDeptIdDto);
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }

    @PostMapping(value = "updateDepartmentById")
    public BaseResponse updateDepartmentById(@RequestBody AdminDepartmentEntity adminDepartmentEntity) {
        String id = departmentService.updateDepartmentById(adminDepartmentEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, id);
    }

    @PostMapping(value = "deleteDepartmentById")
    public BaseResponse deleteDepartmentById(@RequestBody @Validated IdParams idParams) throws DepartmentHasChildrenException {
        departmentService.deleteDepartmentById(idParams);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    @PostMapping(value = "addUserDepartment")
    public BaseResponse addUserDepartment(@RequestBody @Validated AddUserDepartmentDto addUserDepartmentDto) {
        departmentService.addUserDepartment(addUserDepartmentDto);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    @PostMapping(value = "deleteUserDepartment")
    public BaseResponse deleteUserDepartment(@RequestBody @Validated AdminUserDepartmentEntity adminUserDepartmentEntity) {
        departmentService.deleteUserDepartment(adminUserDepartmentEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }
}
